/*
 * Copyright (C) 2013 tangzhi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tangzsoft.pdmviewer;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import org.tangzsoft.pdmviewer.model.PdmFile;
import org.tangzsoft.pdmviewer.model.PdmObject;
import org.tangzsoft.pdmviewer.storage.Storage;
import org.tangzsoft.pdmviewer.utils.Utils;

/**
 * 主窗口上View区域控制器
 * 管理一个多tab页控件，除首页(欢迎页面)外，每个tab页中内容为PdmObjectView
 * 
 * @author tangzhi
 */
public class ViewControler {
    
    private MainView mainView;
    
    private JTabbedPane tpeMain;
    
    private Storage storage =  PdmViewerContext.getInstance().getStorage();
    
    public static List<String> tabs = new ArrayList<String>();
    
    public ViewControler(MainView parent) {
        mainView = parent;
    }
    
    /**
     * 创建欢迎页
     * @return 
     */
    public JScrollPane createWelcomeView() {
        JEditorPane welcome = new JEditorPane();
        
        welcome.setEditable(false);
        JScrollPane welcomeView = new JScrollPane(welcome);
        
        Dimension minimumSize = new Dimension(100, 50);
        welcomeView.setMinimumSize(minimumSize);
        
        welcome.setText("Welcome!");
        
        return welcomeView;
    }
    
    public void init(JTabbedPane parent) {
        
        tpeMain = parent;
        tpeMain.removeAll();
        
        ImageIcon icon;
        icon = Utils.createImageIcon(parent.getClass(),"/images/home.png");
        
        
        tpeMain.addTab("Welcome", icon, createWelcomeView(), "welcome!");
        tpeMain.setTabComponentAt(0, new ButtonTabComponent(parent,false));
        tabs.add("welcome");
        
        //parent.addTab("系统管理22222222222", null, new PdmObjectView());
        //parent.setTabComponentAt(1, new ButtonTabComponent(parent,true));
    }
    
    public void showPdmObjectView(PdmFile pdm, PdmObject diagram) {
        
        String fileId = pdm.getId();
        String diagramId = diagram.getObjectId();
        
        PdmObject[] tables = storage.getPdmObjectDAO().findTables(fileId, diagramId);
        StringBuilder title = new StringBuilder();
        title.append(pdm.getName());
        title.append("[");title.append(diagram.getName());title.append("]");

        String tip = pdm.getPath();
        
        int idx = tabs.indexOf(fileId);
        if (idx>-1) {
            tpeMain.setSelectedIndex(idx);
            PdmObjectView view = (PdmObjectView) tpeMain.getComponentAt(idx);
            view.showPdmObjects(tables);
            tpeMain.setTitleAt(idx, title.toString());
        }else{
            
            tpeMain.addTab(title.toString(), null, new PdmObjectView(tables), tip);
            tpeMain.setTabComponentAt(tpeMain.getTabCount()-1, new ButtonTabComponent(tpeMain,true));
            tpeMain.setSelectedIndex(tpeMain.getTabCount()-1);
            
            tabs.add(fileId);
        }
    }
    
}
